package com.lanyun.auth.controller.admin.authPermission;


import com.google.common.base.Preconditions;
import com.lanyun.auth.controller.admin.authPermission.vo.AuthPermissionRequestVO;
import com.lanyun.auth.domain.authPermission.AuthPermissionDomainService;
import com.lanyun.web.bean.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 权限controller
 *
 * @Author Jqcc
 * @Version 1.0
 * @Data 2025/3/12
 */
@RestController
@RequestMapping("/api-admin/permission")
@Slf4j
public class AuthPermissionController {

    @Resource
    private AuthPermissionDomainService authPermissionDomainService;

    /**
     * 新增权限
     *
     * @param authPermissionRequestVO
     * @return
     */
    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody AuthPermissionRequestVO authPermissionRequestVO) {
        try {

            Preconditions.checkNotNull(authPermissionRequestVO.getPermissionName(), "权限名称不能为空");
            Preconditions.checkNotNull(authPermissionRequestVO.getParentId(), "权限父Id不能为空");


            return Result.success(authPermissionDomainService.add(authPermissionRequestVO));
        } catch (IllegalArgumentException e) {
            log.error("PermissionController.add.error:{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        }
    }


    /**
     * 修改权限信息
     *
     * @param authPermissionRequestVO
     * @return
     */
    @PutMapping("/update")
    public Result<Boolean> update(@RequestBody AuthPermissionRequestVO authPermissionRequestVO) {
        try {

            Preconditions.checkNotNull(authPermissionRequestVO.getId(), "权限Id不能为空");


            return Result.success(authPermissionDomainService.update(authPermissionRequestVO));
        } catch (IllegalArgumentException e) {
            log.error("PermissionController.update.error:{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        }
    }


    /**
     * 删除权限
     *
     * @param authPermissionRequestVO
     * @return
     */
    @PostMapping("/delete")
    public Result<Boolean> delete(@RequestBody AuthPermissionRequestVO authPermissionRequestVO) {
        try {

            Preconditions.checkNotNull(authPermissionRequestVO.getId(), "权限Id不能为空");


            return Result.success(authPermissionDomainService.delete(authPermissionRequestVO));
        } catch (IllegalArgumentException e) {
            log.error("PermissionController.delete.error:{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        }
    }


    /**
     * 查询用户权限
     *
     * @param userName
     * @return
     */
    @PostMapping("/getPermission")
    public Result<List<String>> getPermission(String userName) {
        try {

            return Result.success(authPermissionDomainService.getPermission(userName));
        } catch (Exception e) {
            log.error("PermissionController.getPermission.error:{}", e.getMessage(), e);
            return Result.fail("获取用户权限失败");
        }
    }
}
